package com.cn.nio协议;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Before;
import org.junit.Test;

import javax.jms.*;

/**
 * @author: dlr
 * @data: 2019/9/8 23:28
 * @desc: 点对点队列实例
 */
public class pointTopointProducer {
     private final String MQ_URL="nio://192.168.10.20:61618";
     //点对点对队列名
     private final String QUEUE_NAME="BASE_QUEUE";
     Connection connection=null;
     Session session=null;
    /*
     * @author: dlr
     * @desc:    获取连接
     * @date: 2019/8/11
     * @param
     * @return: void
     */
    @Before
    public void getConnection() throws Exception{
        //连接工厂
        ActiveMQConnectionFactory factory=new ActiveMQConnectionFactory(MQ_URL);
        //获取连接
        connection=factory.createConnection();
        //启动
        connection.start();
        //创建session
        //参数1:是否开启事务,参数2:签收模式
        session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
    }
    /*
     * @author: dlr
     * @desc:   创建消息队列
     * @date: 2019/8/11
     * @param
     * @return: void
     */
    @Test
    public void creatpTp()throws Exception{
      //创建消息目的地点对点队列
      Queue queue=session.createQueue(QUEUE_NAME);
      //创建消息的生产者
      MessageProducer producer = session.createProducer(queue);
      producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      //生产三条消息向队列中
      for(int i=1;i<=3;i++){
          //创建消息
          TextMessage textMessage = session.createTextMessage("第" + i + "条消息");
          //发送到mq的队列中
          producer.send(textMessage);
      }
        System.err.println("success");
        producer.close();
        session.close();
        connection.close();
    }
}
